<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>International Encodings: Ruby Study Notes - Best Ruby Guide, Ruby Tutorial</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="Ruby Study Notes - Best Ruby Guide, Ruby Tutorial" />
<meta name="keywords" content="International Encodings,ruby study notes,free ruby programming guide,ruby guide,free ruby programming course,best ruby guide,ruby tutorials,ruby tutorial,learn ruby,ruby,ruby on rails,ruby rails,ruby learning,ruby tutoring,learning ruby,ruby programming,ruby on rails development,ruby training" />
<meta name="Distribution" content="Global" />
<meta name="author" content="Satish Talim / Original design: Erwin Aligam - ealigam@gmail.com" />
<meta name="copyright" content="Satish Talim 2007 and beyond..." />
<meta name="verify-v1" content="rFu86se+IkbtF+bH8mgJBKwU5HnKaSd8Ghw9umXQOkM=" />
<meta name="robots" content="index,follow" />
<meta http-equiv="Expires" content="0" />
<meta name="revisit-after" content="1 days" />
<link rel="stylesheet" href="/images/NewOrange.css" type="text/css" />
<link rel="stylesheet" href="/images/syntaxhighlighter.css" type="text/css" />
<!-- Google +1 button code -->
<link rel="canonical" href="/satishtalim/international_encodings.html" />
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>

<!-- Google Analytics code -->
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-59044-10']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
<!-- Google Analytics code ends -->
</head>

<body>
<!-- wrap starts here -->
<div id="wrap">

    <div id="header">

        <h1 id="logo">Ruby<span class="orange">Learning.github.io</span></h1>
        <h2 id="slogan">Helping Ruby Programmers become Awesome!</h2>

    </div>

    <div id="menu">
        <ul>
            <li><a href="/" title="Home page for rubylearning.github.io">Home</a></li>
            <li><a href="/satishtalim/tutorial.html" title="Get started Learning Ruby here...">Tutorial</a></li>
            <li><a href="/download/downloads.html" title="Download this tutorial as an eBook">Downloads</a></li>
            <li><a href="/other/testimonials.html" title="People around the world who benefited from this site">Testimonials</a></li>
            <li><a href="/other/certification.html" title="Get certified in Ruby">Certification</a></li>
            <li><a href="/satishtalim/ruby_guide.html" title="Ruby Guide, Mentor">Mentor</a></li>
            <li><a href="/blog/" title="Ruby blog of Ruby Learning site">Blog</a></li>
            <li><a href="/satishtalim/tutorial.html" title="Online Ruby Course">Online Course</a></li>
            <li><a href="http://ruby-challenge.rubylearning.org/" title="Ruby Programming Challenge for Newbies">Challenge</a></li>
            <li><a href="/satishtalim/about.html" title="Information about Satish Talim">About</a></li>
        </ul>
    </div>

    <!-- content-wrap starts here -->
    <div id="content-wrap">

            <div id="main">

                <div id="main-inner"><a name="TemplateInfo"></a>
                <h1>International Encodings</h1>

                <p>By default, the Ruby interpreter assumes that programs are encoded in ASCII. In Ruby 1.9, the author of the script can specify the encoding of the script by placing a special "coding comment" at the start of the file. For example:</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                # coding: utf-8
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>The above comment must be written entirely in ASCII, and must include the string <strong>coding</strong> followed by a colon or equals sign and the name of the desired encoding (which cannot include spaces or punctuation other than hyphen and underscore). Whitespace is allowed on either side of the colon or equals sign, and the string <strong>coding</strong> may have any prefix, such as <strong>en</strong> to spell <strong>encoding</strong>. The entire comment, including coding and the encoding name, is case-insensitive and can be written with upper- or lowercase letters.<br /><br />An encoding comment like the one above is usually valid on the first line of the file. It may appear on the second line, however, if the first line is a shebang comment (which makes a script executable on *nix operating systems):</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                #!/usr/bin/ruby -w
                # coding: utf-8
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>Encoding names are not case-sensitive and may be written in uppercase, lowercase or a mix. Ruby 1.9 supports the following source encodings: ASCII-8BIT, US-ASCII, ISO-8859-1 thro' ISO-8859-15, UTF-8, SHIFT_JIS and EUC-JP.<br /><br />In Ruby 1.9, strings are true sequences of characters, and those characters are not confined to the ASCII character set. In 1.9, the individual elements of a string are characters - represented as strings of length 1 - rather than integer character codes. The <strong>String</strong> class can properly handle multibyte characters. If a string contains multibyte characters, then the number of bytes does not correspond to the number of characters. In Ruby 1.9, the <strong>length</strong> and <strong>size</strong> methods return the number of characters in a string, and the new <strong>bytesize</strong> method returns the number of bytes. Refer the following program (this program is typed using the free Unicode text editor for Windows ie. <a href="http://www.unipad.org/download/">Unipad</a>):</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                # coding: utf-8
                # utf8p1.rb
                # in Ruby 1.9 only
                # A string literal containing a multibye character
                s = "Jos&#233;"
                # The string contains 5 bytes which encode 4 characters
                puts s.length    # => 4
                puts s.bytesize # => 5
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>The above information is adapted from the book - <em>The Ruby Programming Language</em>.</p>

                <p>Dave Thomas has an interesting example on his <a href="http://pragdave.blogs.pragprog.com/pragdave/2008/04/fun-with-ruby-1.html">blog</a>.</p>

                <h3>Using both Ruby 1.8 and 1.9 on Windows</h3>

                <p>You can use both Ruby 1.8 and 1.9 on Windows. This is what you need to do to use Ruby 1.9 -</p>

                <ul>
                  <li>Download Ruby 1.9 for Windows, from -<br /><a href="http://www.ruby-lang.org/en/downloads/">http://www.ruby-lang.org/en/downloads/</a><br /><br />On this page click on the link for -<br /><a href="ftp://ftp.ruby-lang.org/pub/ruby/binaries/mswin32/unstable/ruby-1.9.0-0-i386-mswin32.zip">Ruby 1.9.0 Binary</a></li>
                  <li>Then, unzip the downloaded file to a folder say - c:/ruby1.9</li>
                  <li><strong>Mind you, this download does not have additional tools like SciTE, so you may have to use something like Wordpad or Textpad for writing your .rb files</strong>.</li>
                  <li>Open a command window and switch folder to: c:/ruby1.9/bin</li>
                  <li>In this command window, type -<br /><strong>set path=c:/ruby1.9/bin;</strong><br />(<strong>Remember</strong>: This path setting is valid as long as this command window is open.)</li>
                  <li>Switch to the folder where your Ruby programs are located, say c:/rubyprograms and then you can compile any Ruby program by typing -<br /><strong>ruby program.rb</strong></li>
                </ul>

                <!--
                <p class="post-footer align-right">
                  <strong>
                    <a href="/satishtalim/ruby_names.html">&lt;Names | </a>
                    <a href="/satishtalim/tutorial.html">TOC | </a>
                    <a href="/satishtalim/writing_own_ruby_methods.html">Writing own Ruby Methods&gt;</a>
                  </strong>
                </p>
                -->
            </div>
            <!-- main inner ends here -->
        </div>

            <div id="rightbar">

            </div>

    <!-- content-wrap ends here -->
    </div>

<!-- wrap ends here -->
</div>

<!-- footer starts here -->
<div id="footer">
    <!-- CHANGE THE FOOTER -->
    <p>&copy; 2006-2021 <strong>rubylearning.github.io - A Ruby Tutorial</strong>&nbsp;&nbsp;Page Updated: 5th Jan. 2021 | Design: <a href="mailto:ealigam@gmail.com">Erwin Aligam</a> | Valid: <a href="http://validator.w3.org/check/referer">XHTML</a> | <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/">Home</a> | <a href="/privacy.html">Privacy</a> | <a href="/sitemap.html">Sitemap</a></p>
</div>

<!-- SyntaxHighlighter code -->
<script src="/js/shCore.js" type="text/javascript"></script>
<script src="/js/shBrushRuby.js" type="text/javascript"></script>
<script type="text/javascript">
dp.SyntaxHighlighter.HighlightAll('code');
</script>
<!-- SyntaxHighlighter code -->
</body>
</html>
